import Rx from "rxjs"

//模拟缓存数据
let subject1 = new Rx.BehaviorSubject("").map(value=>{console.log(value);return value;}).map(value => acc => Object.assign({}, acc, { num4: value }));
subject1.next(1);

let observable1 = buildObservable("No.1", 1000).map(value=>{console.log(value);return value;}).map(value => acc => Object.assign({}, acc, { num1: value }));
let observable2 = buildObservable("No.2", 2000).map(value=>{console.log(value);return value;}).map(value => acc => Object.assign({}, acc, { num2: value }));
let observable3 = buildObservable("No.3", 3000).map(value=>{console.log(value);return value;}).map(value => acc => Object.assign({}, acc, { num3: value }));

//模拟cometd更新数据
setTimeout(() => {
    subject1.next(2);
}, 3500);

function buildObservable(name, timeout) {
    return Rx.Observable.create(observer => {
        setTimeout(() => {
            observer.next("name is: " + name);
        }, timeout)
    });
}

//模拟等待所有数据都回来后渲染表格
Rx.Observable.merge(observable1, observable2, observable3, subject1)
    .scan((acc, changeFn) => {
        return changeFn(acc)
    }, {})
    .filter(value => {
        return value.num1 != undefined && value.num2 != undefined && value.num3 != undefined && value.num4 != undefined
    })
    .subscribe((x) => {
        console.log(x);
    })